import 'package:cashbook/main.dart';
import 'package:cashbook/model/Accounts.dart';
import 'package:cashbook/model/eventbus_bean.dart';
import 'package:cashbook/utils/Commons.dart';
import 'package:cashbook/utils/manager.dart';
import 'package:cashbook/utils/time_util.dart';
import 'package:cashbook/utils/toast.dart';
import 'package:cashbook/view/ChartPage.dart';
import 'package:cashbook/view/MePage.dart';
import 'package:cashbook/view/RecordPage.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return MyHomeState();
  }
}

class MyHomeState extends State<MyHomePage> {
  int _currentIndex = 0;
  var cashtitle = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: currentPage(),
      bottomNavigationBar: BottomNavigationBar(
        onTap: ((index) {
          setState(() {
            _currentIndex = index;
          });
        }),
        items: [
          BottomNavigationBarItem(
              backgroundColor: Colors.white,
              icon: _currentIndex == 0
                  ? Image.asset(
                      'images/detail_pre.png',
                      width: 32,
                      height: 28,
                    )
                  : Image.asset(
                      'images/detail_def.png',
                      width: 32,
                      height: 28,
                    ),
              title: Text(
                '明细',
                style: TextStyle(
                    color:
                        _currentIndex == 0 ? Colors.blue : Color(0xff999999)),
              )),
          BottomNavigationBarItem(
              backgroundColor: Colors.white,
              icon: _currentIndex == 1
                  ? Image.asset(
                      'images/chart_pre.png',
                      width: 32,
                      height: 28,
                    )
                  : Image.asset(
                      'images/chart_def.png',
                      width: 32,
                      height: 28,
                    ),
              title: Text(
                '报表',
                style: TextStyle(
                    color:
                        _currentIndex == 1 ? Colors.blue : Color(0xff999999)),
              )),
          BottomNavigationBarItem(
              backgroundColor: Colors.white,
              icon: _currentIndex == 2
                  ? Image.asset(
                      'images/me_pre.png',
                      width: 32,
                      height: 28,
                    )
                  : Image.asset(
                      'images/me_def.png',
                      width: 32,
                      height: 28,
                    ),
              title: Text(
                '我的',
                style: TextStyle(
                    color:
                        _currentIndex == 2 ? Colors.blue : Color(0xff999999)),
              )),
        ],
        type: BottomNavigationBarType.fixed,
      ),
    );
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    Manager.instance.isLogin = false;
  }

  RecordPage recordPage;
  MePage mePage;
  ChartPage chartPage;

  currentPage() {
    switch (_currentIndex) {
      case 0:
        if (recordPage == null) {
          recordPage = RecordPage();
        }
        return recordPage;
      case 2:
        if (mePage == null) {
          mePage = MePage();
        }
        return mePage;
      case 1:
        if (chartPage == null) {
          chartPage = ChartPage();
        }
        return chartPage;
    }
  }

  void addAlertDialog(BuildContext context, Function sure) {
    showDialog(
        context: context,
        builder: (_) => AlertDialog(
              title: Text('添加一个账目'),
              content: TextField(
                controller: cashtitle,
                decoration: InputDecoration(hintText: '名称'),
              ),
              actions: <Widget>[
                new FlatButton(
                  child: new Text("取消"),
                  onPressed: () {
                    Navigator.of(context).pop();
                  },
                ),
                new FlatButton(
                  child: new Text("添加"),
                  onPressed: () {
                    sure();
                    Navigator.of(context).pop();
                  },
                )
              ],
            ));
  }

  void addAccount() async {
//    if (null == cashtitle || cashtitle.text.trim().toString().length == 0) {
//      Toast.toast(context, '账目名称不能为空！');
//      return;
//    }
//    Accounts accounts = Accounts();
//    accounts.title = cashtitle.text.trim().toString();
//    accounts.createTime = TimeUtil.currentTimeMillis().toString();
//    accounts.type = "";
//    accounts.money = '0';
//
//    int res = await AccountsProvider().insert(accounts);
//    if (-1 != res) {
//      Toast.toast(context, '添加成功！');
//      var bean = EventBusBean(Commons.REFRESH_ACCOUNTS, "");
//      Manager.instance.eventBus.fire(bean);
//    } else {
//      Toast.toast(context, '添加失败！');
//    }
  }
}
